﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.UI
{
    public class PageBase : Base
    {
        #region 属性

        /// <summary>
        /// 条件参数
        /// </summary>
        internal List<DBParameter> Parameters
        {
            get
            {
                if (ViewState["Parameters"] == null)
                {
                    List<DBParameter> list = new List<DBParameter>();
                    ViewState["Parameters"] = list;
                }
                return (List<DBParameter>)ViewState["Parameters"];
            }
            set
            {
                ViewState["Parameters"] = value;
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 清空参数
        /// </summary>
        public void ClearParameters()
        {
            Parameters = null;
        }

        public List<DBParameter> GetParameters()
        {
            List<DBParameter> result = new List<DBParameter>();
            foreach (DBParameter parameter in Parameters)
            {
                result.Add(parameter);
            }

            return result;
        }
        /// <summary>
        /// 添加参数
        /// </summary>
        internal void AddDBParameter(List<DBParameter> list, string parameterName, object value)
        {
            var infos = from temp in list
                        where temp.ParameterName.ToLower() == parameterName.ToLower()
                        select temp;
            if (infos.ToArray<DBParameter>().Length > 0)
            {
                list.Remove((DBParameter)infos.ToArray<DBParameter>()[0]);
            }
            list.Add(new DBParameter(parameterName, value));
        }

        /// <summary>
        /// 添加数据库参数
        /// </summary>
        /// <param name="parameterName">参数名称</param>
        /// <param name="objValue">参数值</param>
        public void AddDBParameter(string parameterName, object objValue)
        {
            AddDBParameter(this.Parameters, parameterName, objValue);
        }

        internal override void InitializeComponent()
        {
            base.InitializeComponent();
            FindControl();
            RegEvents();
        }

        /// <summary>
        /// 查找页面控件
        /// </summary>
        internal virtual void FindControl()
        {

        }
        /// <summary>
        /// 注册控件
        /// </summary>
        internal virtual void RegEvents()
        {
        }

        #endregion

        #region 事件

        /// <summary>
        /// 页面异常
        /// </summary>
        //private void Page_Error(object sender, EventArgs e)
        //{
        //    Exception ex = Server.GetLastError();
        //    if (ex is HttpRequestValidationException)
        //    {
        //        Response.Write("<script language='JavaScript'>alert('请您输入合法字符串!');window.history.go(-1);</script>");
        //        Server.ClearError(); // 如果不ClearError()这个异常会继续传到Application_Error()。
        //    }
        //}



        #endregion
    }
}
